package net.bblot.lottery.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;

import com.marsscp.cp.R;
import net.bblot.lottery.customView.MyViewPager;
import net.bblot.lottery.util.AppConfig;

import java.util.HashMap;

public class GuideViewActivity extends BaseActivity {
	private final static String TAG = "GuideViewActivity";

	private ImageView iv_num_select;
	private View btn_gohome;
	private MyViewPager vp;
	private HashMap<Integer, View> view_map;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_guide);
		bindView();
		init();
		setListener();
	}

	private void setListener() {
		// TODO Auto-generated method stub

	}

	private void init() {
		// TODO Auto-generated method stub
		vp.setAdapter(new GuideViewAdapter());
		if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
			View v = this.getWindow().getDecorView();
			v.setSystemUiVisibility(View.GONE);
		} else if (Build.VERSION.SDK_INT >= 19) {
			//for new api versions.
//			View decorView = getWindow().getDecorView();
//			int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//					| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
//			decorView.setSystemUiVisibility(uiOptions);
		}
		
	}

	private void bindView() {
		// TODO Auto-generated method stub
		vp = (MyViewPager) findViewById(R.id.vp);

		btn_gohome=findViewById(R.id.btn_go_home);
		vp.addOnPageChangeListener(new MyOnPageChangerListener());
	}
	
	private class MyOnPageChangerListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			//

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
//			if (position == 0) {
//				iv_num_select.setImageResource(R.drawable.guide_page_icon_01);
//			} else if (position == 1) {
//				iv_num_select.setImageResource(R.drawable.guide_page_icon_02);
//			} else if (position == 2) {
//				iv_num_select.setImageResource(R.drawable.guide_page_icon_03);
//			}
			if (position == 2) {
//				vp.setCan_scrool(false);
//				new Handler().postDelayed(new Runnable() {
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						AppConfig.saveFirstUsage(getApplicationContext(), false);
//						AppManager.getAppManager().intentToAtcivity(GuideViewActivity.this, MainActivity.class);
//					}
//				}, 2000);
		}}
	}

	
	class GuideViewAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// ((ViewPager) container).removeView(viewPageList.get(position));
			// super.destroyItem(container, position, object);
			// ((ViewPager) container).removeViewAt(position);
		}

		@Override
		public Object instantiateItem(View container, int position) {
			View v = getLayoutInflater().inflate(R.layout.guide_page_item, null);
			ImageView iv_bg = (ImageView) v.findViewById(R.id.page_item_iv_bg);
			ImageView iv_num_1 = (ImageView)v.findViewById(R.id.page_num_iv1);
			ImageView iv_num_2 = (ImageView) v.findViewById(R.id.page_num_iv2);
			ImageView iv_num_3 = (ImageView) v.findViewById(R.id.page_num_iv3);
			View btn_go_home=  v.findViewById(R.id.btn_go_home);
			if(position==0){
				iv_num_1.setImageResource(R.drawable.guide_point_selected);
				iv_num_2.setImageResource(R.drawable.guide_point_unselect);
				iv_num_3.setImageResource(R.drawable.guide_point_unselect);
				btn_go_home.setVisibility(View.INVISIBLE);
			}else if(position==1){
				iv_num_2.setImageResource(R.drawable.guide_point_selected);
				iv_num_1.setImageResource(R.drawable.guide_point_unselect);
				iv_num_3.setImageResource(R.drawable.guide_point_unselect);
				btn_go_home.setVisibility(View.INVISIBLE);

			}else if(position==2){
				iv_num_3.setImageResource(R.drawable.guide_point_selected);
				iv_num_2.setImageResource(R.drawable.guide_point_unselect);
				iv_num_1.setImageResource(R.drawable.guide_point_unselect);
				btn_go_home.setVisibility(View.VISIBLE);
			}

			btn_go_home.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AppConfig.saveFirstUsage(getApplicationContext(), false);
					AppManager.getAppManager().intentToAtcivity(GuideViewActivity.this, MainActivity.class);
				}
			});
			switch (position) {
			case 0:
				iv_bg.setImageResource(R.drawable.guide_page1);
//				iv_bottom.setImageResource(R.drawable.iv_page1_des);
				break;
			case 1:
				iv_bg.setImageResource(R.drawable.guide_page2);
//				iv_bottom.setImageResource(R.drawable.iv_page2_des);
				break;
			case 2:
//				iv_bottom.setImageResource(R.drawable.iv_page3_des);
				iv_bg.setImageResource(R.drawable.guide_page3);
				break;

			default:
				break;
			}
			// view_map.put(position, v);
			((ViewPager) container).addView(v, 0);
			return v;
		}
	}

}
